﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace TIMETABLEASY.Controler
{
    public class TimeTable : Control
    {
        static TimeTable()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TimeTable), new FrameworkPropertyMetadata(typeof(TimeTable)));

            CommandManager.RegisterClassCommandBinding(typeof(TimeTable), new CommandBinding(NextDay, new ExecutedRoutedEventHandler(OnExecutedNextDay), new CanExecuteRoutedEventHandler(OnCanExecuteNextDay)));
            CommandManager.RegisterClassCommandBinding(typeof(TimeTable), new CommandBinding(PreviousDay, new ExecutedRoutedEventHandler(OnExecutedPreviousDay), new CanExecuteRoutedEventHandler(OnCanExecutePreviousDay)));
            
        }

        #region MondayCurrentDate

        public static readonly DependencyProperty MondayCurrentDateProperty = 
            DependencyProperty.Register("MondayCurrentDate", typeof(DateTime), typeof(TimeTable),
                new FrameworkPropertyMetadata((DateTime)DateTime.Now, onMondayCurrentDateChanged));

        public DateTime MondayCurrentDate
        {
            get { return (DateTime)GetValue(MondayCurrentDateProperty); }
            set { SetValue(MondayCurrentDateProperty, value); }
        }

        protected static void onMondayCurrentDateChanged(DependencyObject obj,DependencyPropertyChangedEventArgs args)
        {
            ((TimeTable)obj).onMondayCurrentDateChanged(args);
        }

        protected virtual void onMondayCurrentDateChanged(DependencyPropertyChangedEventArgs e)
        {
            DateTime byDate = MondayCurrentDate;
            TextBlock headerMonday = this.GetTemplateChild("headerMonday") as TextBlock;
            headerMonday.Text = byDate.DayOfWeek.ToString();
        }

        #endregion

        #region previous/next

        public static readonly RoutedCommand NextDay = new RoutedCommand("NextDay", typeof(TimeTable));
        public static readonly RoutedCommand PreviousDay = new RoutedCommand("PreviousDay", typeof(TimeTable));
 
        private static void OnCanExecuteNextDay(object sender, CanExecuteRoutedEventArgs e)
        {
            ((TimeTable)sender).OnCanExecuteNextDay(e);
        }

        private static void OnExecutedNextDay(object sender, ExecutedRoutedEventArgs e)
        {
            ((TimeTable)sender).OnExecutedNextDay(e);
        }

        protected virtual void OnCanExecuteNextDay(CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = false;
        }

        protected virtual void OnExecutedNextDay(ExecutedRoutedEventArgs e)
        {
            MondayCurrentDate += TimeSpan.FromDays(1);

            CalendarDay monday = this.GetTemplateChild("Monday") as CalendarDay;
            CalendarDay tuesday = this.GetTemplateChild("Tuesday") as CalendarDay;
            CalendarDay wednesday = this.GetTemplateChild("Wednesday") as CalendarDay;
            CalendarDay thursday = this.GetTemplateChild("Thursday") as CalendarDay;
            CalendarDay friday = this.GetTemplateChild("Friday") as CalendarDay;
            CalendarDay saturday = this.GetTemplateChild("Saturday") as CalendarDay;
            CalendarDay sunday = this.GetTemplateChild("Sunday") as CalendarDay;

            monday.nextWeek();
            tuesday.nextWeek();
            wednesday.nextWeek();
            thursday.nextWeek();
            friday.nextWeek();
            saturday.nextWeek();
            sunday.nextWeek();
            
            e.Handled = true;
        }

        private static void OnCanExecutePreviousDay(object sender, CanExecuteRoutedEventArgs e)
        {
            ((TimeTable)sender).OnCanExecutePreviousDay(e);
        }

        private static void OnExecutedPreviousDay(object sender, ExecutedRoutedEventArgs e)
        {
            ((TimeTable)sender).OnExecutedPreviousDay(e);
        }

        protected virtual void OnCanExecutePreviousDay(CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = false;
        }

        protected virtual void OnExecutedPreviousDay(ExecutedRoutedEventArgs e)
        {
            MondayCurrentDate -= TimeSpan.FromDays(1);
            
            CalendarDay monday = this.GetTemplateChild("Monday") as CalendarDay;
            CalendarDay tuesday = this.GetTemplateChild("Tuesday") as CalendarDay;
            CalendarDay wednesday = this.GetTemplateChild("Wednesday") as CalendarDay;
            CalendarDay thursday = this.GetTemplateChild("Thursday") as CalendarDay;
            CalendarDay friday = this.GetTemplateChild("Friday") as CalendarDay;
            CalendarDay saturday = this.GetTemplateChild("Saturday") as CalendarDay;
            CalendarDay sunday = this.GetTemplateChild("Sunday") as CalendarDay;

            monday.previousWeek();
            tuesday.previousWeek();
            wednesday.previousWeek();
            thursday.previousWeek();
            friday.previousWeek();
            saturday.previousWeek();
            sunday.previousWeek();

            e.Handled = true;
        }

        #endregion

    }
}
